package com.nwpusp.transmitter.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.nwpusp.controller.actions.Action;
import com.nwpusp.model.session.ConnectionId;
import com.nwpusp.transmitter.Transmitter;

import android.util.Log;


public class AsClient {
	
	private static final int Port = 1818;
	private Socket socket;
	private SocketAddress socketAddress;
	private ObjectInputStream objInputStream = null;
	private ObjectOutputStream objOutputStream = null;
	private Transmitter transmitter;
	private String SeverIPAddress;
	private ConnectionId Serverip;
	private boolean stopClient = false;

	public AsClient(Transmitter transmitter){
		this.transmitter = transmitter;
	}
	
	public void startClient(ConnectionId host){
		stopClient = false;
		this.Serverip = host;
		Long ip = host.getCid();
		SeverIPAddress = String.valueOf(ip & 0xFF)+"."
				+String.valueOf((ip >> 8 ) & 0xFF)+"."
				+String.valueOf((ip >> 16 ) & 0xFF)+"."
				+String.valueOf((ip >> 24 ) & 0xFF);
		Log.d("aob_ipmsg", SeverIPAddress);
		Thread thread = new Thread(runnable);
		thread.start(); 
	}
	public Runnable runnable = new Runnable(){
		public void run(){
			try {
				socket=new Socket();
				socketAddress = new InetSocketAddress(SeverIPAddress,Port);
				socket.connect(socketAddress,5000); 
	    		Log.d("aob_ipmsg", "connect");
				Log.d("aob_ipmsg", "connect success");
				transmitter.notifyEstablished(Serverip, true);
			}catch (UnknownHostException e) {
				e.printStackTrace();
				Log.d("aob_ipmsg", "Net error");
				transmitter.notifyEstablished(Serverip, false);
			} catch (IOException e){
				e.printStackTrace();
				Log.d("aob_ipmsg", "Net error");
				transmitter.notifyEstablished(Serverip, false);
			}
			receiver();
		}
	};

	public void sender(ArrayList<Action> al){
		try{
			if(objOutputStream == null)
				objOutputStream = new ObjectOutputStream(socket.getOutputStream());    
	        objOutputStream.writeObject(al);  
	        objOutputStream.flush();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void receiver(){
		try{
			if(objInputStream == null) 
				objInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
			while(!stopClient){
				ArrayList<Action> al = (ArrayList<Action>) objInputStream.readObject();
				Log.d("JustinAlNum", "client recv al num: " + String.valueOf(al.size()));
				transmitter.notifyReceive(Serverip,al);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stopClient(){
		stopClient = true;
		try {
			if(socket!=null) {
				socket.close();
				socket = null;
			}
			if(objInputStream!=null) {
				objInputStream.close();
				objInputStream = null;
			}
			if(objOutputStream!=null) {
				objOutputStream.close();
				objOutputStream = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
